<?php
class UserController extends ApplicationController {
  public function signup() {
    //TODO: Remove hack
    $params_user = (isset($this->params['user'])) ? $this->params['user'] : array();
    $this->user = new User($params_user);
    //var_dump($params_user);
    if($this->request()->post()) {
      if($this->user->save()) {
        $u = User::authenticate($this->user->login, $this->user->password);
        $_SESSION['user_id'] = $u->id;
        //$_SESSION['user'] = User::authenticate($this->user->login, $this->user->password);
        $this->flash['message'] = 'Signup successful';
        $this->redirect_to(array('controller' => 'user', 'action' => 'welcome'));
      } else {
        $this->flash['warning'] = 'Signup unsuccessful';
      }
    }
  }
  
  public function login() {
    if($this->request()->post()) {
      if($this->current_user = User::authenticate($this->params['user']['login'], $this->params['user']['password'])) {
        $_SESSION['user_id'] = $this->current_user->id;
        $this->flash['message'] = 'Login successful';
      } else {
        $this->flash['warning'] = 'Login unsuccessful';
      }
    }
  }
  
  public function logout() {
    $_SESSION['user_id'] = null;
    //$_SESSION['user'] = null;
    $this->flash['message'] = 'Logged out';
    $this->redirect_to(array('controller' => 'user', 'action' => 'login'));
  }
  
  public function forgot_password() {
    if($this->request()->post()) {
      $u = User::find_by_email($this->params['user']['email']);
      if($u and $u->send_new_password()) {
        $this->flash['message'] = 'A new password has been sent by email';
        $this->redirect_to(array('controller' => 'user', 'action' => 'login'));
      } else {
        $this->flash['warning'] = 'Could not send password';
      }
    }
  }
  
  public function change_password() {
   $user = $_SESSION['user'];
   if($this->request()->post()) {
     $user.update_attributes(array('password' => $this->params['user']['pasword'], 'password_confirmation' => $this->params['user']['pasword_confirmation']));
     if($user->save()) {
       $this->flash['message'] = 'Password changed';
     }
   }
  }
  
  public function welcome() {}
  public function hidden() {}
  
  public function index() {
    $this->redirect_to(array('controller' => 'user', 'action' => 'enlist'));
  }
  
  public function enlist() {
    $this->users = User::find('all');
  }
  
  public function add() {
    //TODO: Remove hack
    $params_obj = (isset($this->params['user'])) ? $this->params['user'] : array();
    $this->user = new User($params_obj);
    if($this->request()->post()) {
      if($this->user->save()) {
        $this->flash['notice'] = 'Saved new User';
        $this->redirect_to(array('controller' => 'user', 'action' => 'enlist'));
      }  
    }
  }

  public function view() {
    $this->user = User::find_by_id($this->params['id'], array('include' => User::$default_includes));
  }
  
  public function edit() {
    $this->user = User::find($this->params['id'], array('include' => User::$default_includes));
  }
  public function update() {
    $user = User::find($this->params['id'], array('include' => User::$default_includes));
    $user->set_attributes($this->params['user']);
    if($user->save()) {
      $this->flash['notice'] = "Updated User {$user->id}";
      $this->redirect_to(array('controller' => 'user', 'action' => 'enlist'));
    }
  }
  
  public function delete() {
    if(!$user = User::find_by_id($this->params['id'])) {
      $this->flash['notice'] = "Could not find User with ID {$this->params['id']}";
      $this->redirect_to(array('controller' => 'user', 'action' => 'enlist'));
    }
    
    if($user->delete()) {
      $this->flash['notice'] = "Deleted User {$user->id}";
    } else {
      $this->flash['notice'] = "Could not delete User {$user->id}";
    } 
    $this->redirect_to(array('controller' => 'user', 'action' => 'enlist')); 
  }  
}